﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Xml.Serialization;
using DeSleeper.Client.Properties;
using DeSleeper.Client.WakeUp;
using MbUnit.Framework;
using MbUnit.Framework.Xml;

namespace DeSleeper.Tests
{
    [TestFixture]
    public class TargetComputerFixture
    {
        [Test]
        public void LoadAndSaveFromSettings()
        {
            var expected = new TargetComputer { Destination = new ProxyWakeUpDestination { ProxyAddress = "TestAddress" }, WakeUpMethod = new LocalWakeUpMethod() };
            TargetComputer.All.Add(expected);
            Settings.Default.Save();
            Settings.Default.Reload();
            Assert.IsNotNull(Settings.Default.Targets);
            var result = TargetComputer.All.Last();
            expected.AssertEqual(result);
        }


        [Test]
        public void SerializeTargetComputerLocalWakeUp()
        {
            var expected = new TargetComputer {Destination = new ProxyWakeUpDestination {ProxyAddress = "TestAddress"}, WakeUpMethod = new LocalWakeUpMethod()};
            var expectedMacAddress = "00-07-E9-38-46-2C";
            expected.SetValue(LocalWakeUpMethod.MacAddressProperty, expectedMacAddress);

            var serializer = new XmlSerializer(typeof(TargetComputer));

            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, expected);
                Console.WriteLine(writer.GetStringBuilder().ToString());
                using (var reader = new StringReader(writer.GetStringBuilder().ToString()))
                {
                    var output = serializer.Deserialize(reader) as TargetComputer;
                    expected.AssertEqual(output);
                }
            }
        }

        [Test]
        public void SerializeTargetComputers()
        {
            var targetComputers = new List<TargetComputer>();
            var expected = new TargetComputer { Destination = new ProxyWakeUpDestination { ProxyAddress = "TestAddress" } };
            targetComputers.Add(expected);
            var serializer = new XmlSerializer(typeof(List<TargetComputer>));

            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, targetComputers);
                Console.WriteLine(writer.GetStringBuilder().ToString());
                using (var reader = new StringReader(writer.GetStringBuilder().ToString()))
                {
                    var outputDestination = serializer.Deserialize(reader) as List<TargetComputer>;
                    var output = outputDestination.First();
                    expected.AssertEqual(output);
                }
            }
        }

        [Test]
        public void SetMethod()
        {
            var targetComputer = new TargetComputer { Destination = new ProxyWakeUpDestination { ProxyAddress = "TestAddress" }};
            var expectedMethod = targetComputer.Destination.Methods.First();
            targetComputer.WakeUpMethod = expectedMethod;
            expectedMethod.AssertEqual(targetComputer.WakeUpMethod);
        }

        [Test]
        public void SetMethodAndFreeze()
        {
            var targetComputer = new TargetComputer { Destination = new ProxyWakeUpDestination { ProxyAddress = "TestAddress" } };
            var expectedMethod = targetComputer.Destination.Methods.First();
            targetComputer.WakeUpMethod = expectedMethod;
            targetComputer.Freeze();
            expectedMethod.Freeze();
            testThread(() => expectedMethod.AssertEqual(targetComputer.WakeUpMethod));
        }

        private void testThread(ThreadStart start)
        {
            var asyncResult = start.BeginInvoke(null, null);
            start.EndInvoke(asyncResult);
        }
    }
}
